Some of you have already been working in blogdown. But there are other ways that you can make your RMarkdown materials available for others to see. We’ll create a repo that contains .Rmd and knitted .html files that we can share as web pages!
Note: The gh-pages section of this lab is inspired by materials from Dr. Julia Lowndes (https://jules32.github.io/).
Julia has written a post on how to make website with RMarkdown and GitHub here: https://jules32.github.io/rmarkdown-website-tutorial/
Follow along with these steps to make your gh-pages branch:
gh-pages, and create a new branchgh-pages2 branches and delete the master branchyour-user-name.github.io/repo-name/data folder for this week into your projectus-renewables.Rmd in the projectIn that .Rmd:
# For general stuff:
library(tidyverse)
library(janitor)
library(lubridate)
library(here)
library(paletteer)
# For ts stuff:
library(tsibble)
library(fable)
library(fabletools)
library(feasts)
library(forecast)
# For spatial stuff:
library(sf)
library(tmap)
library(mapview)
We’ll explore, then forecast, US energy consumption and production by renewables source. Get the data from renewables_cons_prod.csv:
us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>%
clean_names()
Explore the data frame:
# View(us_renew)
# names(us_renew)
# unique(us_renew$description)
We’ll focus on consumption data.
renew_clean <- us_renew %>%
mutate(description = str_to_lower(description)) %>%
filter(str_detect(description, pattern = "consumption")) %>%
filter(!str_detect(description, pattern = "total"))
yyyymm column to date with lubridaterenew_date <- renew_clean %>%
mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>%
mutate(month_sep = yearmonth(yr_mo_day)) %>% #coerce to tsibble `yearmonth` format
mutate(value = as.numeric(value)) %>%
drop_na(month_sep, value)
# Want to parse the year and month? We may use this later...
renew_parsed <-renew_date %>%
mutate(month = month(yr_mo_day, label = TRUE)) %>%
mutate(year = year(yr_mo_day))
Make, then save, the baseline plot as renew_gg:
renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value, group = description)) +
geom_line(aes(color = description)) +
theme_minimal() +
scale_y_continuous(limits = c(0, 350))
renew_gg
Now try updating your color palette using options from paletteer. Use View(palettes_d_names) to see all of the discrete scale options. We’ll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding scale_color_paletteer_d("package::palette"). Like, if I want to use the calecopal::figmtn palette, I’d add:
renew_gg + scale_color_paletteer_d("calecopal::figmtn")
Try some out!
renew_gg +
scale_color_paletteer_d("calecopal::figmtn")
Have some fun trying out different color palettes.
renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)
renew_ts %>% autoplot(value)
renew_ts %>% gg_subseries(value)
renew_ts %>% gg_season(value)
# What if gg_season() didn't work? Well we can make this with ggplot anyway!
# Remember our other parsed version (renew parsed):
ggplot(data = renew_parsed, aes(x = month, y = value, group = year)) +
geom_line(aes(color = year)) +
facet_wrap(~ description,
ncol = 1,
scales = "free",
strip.position = "right")
hydro_ts <- renew_ts %>%
filter(description == "hydroelectric power consumption")
# Explore:
hydro_ts %>% autoplot(value)
hydro_ts %>% gg_subseries(value)
hydro_ts %>% gg_season(value)
# OK, what if gg_season() doesn't work?
# It's just a function that uses ggplot() to do things we already know how to do in ggplot()!
ggplot(hydro_ts, aes(x = month, y = value, group = year)) +
geom_line(aes(color = year))
index_by()What if we want to calculate consumption by quarter? We’ll use index_by() to tell R which “windows” to calculate a value with in.
Quarterly:
hydro_quarterly <- hydro_ts %>%
index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates
summarise(
avg_consumption = mean(value)
)
head(hydro_quarterly)
## # A tsibble: 6 x 2 [1Q]
## year_qu avg_consumption
## <qtr> <dbl>
## 1 1973 Q1 261.
## 2 1973 Q2 255.
## 3 1973 Q3 212.
## 4 1973 Q4 225.
## 5 1974 Q1 292.
## 6 1974 Q2 290.
Or annually:
hydro_annual <- hydro_ts %>%
index_by(annual = ~year(.)) %>%
summarize(
avg_consumption = mean(value)
)
ggplot(data = hydro_annual, aes(x = annual, y = avg_consumption)) +
geom_line(color = "darkorange") +
geom_smooth(color = "purple",
size = 0.2,
linetype = "dashed",
fill = "purple",
alpha = 0.2) +
theme_minimal()
And if you have higher interval data (e.g. hourly), then you can calculate summaries by week, month, etc. using functions from tsibble like:
yearweekyearmonthFirst, let’s check the decomposition (STL):
# Find STL decomposition
dcmp <- hydro_ts %>%
model(STL(value ~ season(window = 5)))
# View the components
# components(dcmp)
# Visualize the decomposed components
components(dcmp) %>% autoplot() +
theme_minimal()
# Let's check out the residuals:
hist(components(dcmp)$remainder)
hydro_ts %>%
ACF(value) %>%
autoplot()
We see peaks at 12 months: annual-difference similarities in consumption.
hydro_model <- hydro_ts %>%
model(
arima = ARIMA(value),
ets = ETS(value)
) %>%
fabletools::forecast(h = "2 years")
hydro_model %>%
autoplot(filter(hydro_ts,
year(month_sep) > 2010),
level = NULL)
A world map with bubbles!
# Get spatial data:
world <- read_sf(dsn = here("data","TM_WORLD_BORDERS_SIMPL-0.3-1"), layer = "TM_WORLD_BORDERS_SIMPL-0.3") %>% clean_names()
# Quick & easy option to see those polygons (also for points, lines!)
mapview(world)
# ggplot (static)
world_base <- ggplot(data = world) +
geom_sf(aes(fill = pop2005),
color = NA) +
scale_fill_paletteer_c("viridis::viridis") +
theme_minimal()
world_base
# Let's crop it:
world_base +
coord_sf(xlim = c(-20, 50), ylim = c(-40, 40), expand = FALSE)
Since you are working in a gh-pages branch, this can be a webpage!
You should see the webpage containing your knitted document!
Troubleshooting: - Don’t including a trailing slash (404 error) - Don’t include the .Rmd extension (will ask to download .Rmd)